package digitalmat.p.com.google.code;

import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;



public class GUIMain {

   private static JMenuBar createMenuBar(JFrame frame) {
      JMenuBar menu_bar = new JMenuBar();

      JMenu menu1 = new JMenu("Menu1");

      JMenu menu11 = new JMenu("Menu11");
      JMenuItem item1 = new JMenuItem("item1");
      JMenuItem item2 = new JMenuItem("item2");
      menu11.add(item1);
      menu11.add(item2);
      menu1.add(menu11);

      JMenuItem item3 = new JMenuItem("item3");
      menu1.add(item3);

      JMenu menu2 = new JMenu("Menu2");
      JMenuItem item4 = new JMenuItem("item4");
      menu2.add(item4);

      JMenu menu3 = new JMenu("Menu3");
      JMenuItem item5 = new JMenuItem("item5");
      menu3.add(item5);

      final JFrame the_frame = frame;
      item5.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent ae) {
            System.out.println("Allo");
            int option = JOptionPane.showConfirmDialog(the_frame, "Allo");
            }
         });

      menu_bar.add(menu1);
      menu_bar.add(menu2);
      menu_bar.add(menu3);

      return menu_bar;
      }

   private static JComponent createTools() {
      JComponent tools = new EditPanel();
      return tools;
      }

   private static StatusBar createStatus() {
      return new StatusBar();
      }

   private static JComponent createDraw(final StatusBar status) {
      final DrawPanel draw = new DrawPanel();

      draw.addMouseMotionListener(new MouseMotionAdapter() {
         @Override
         public void mouseMoved(MouseEvent me) {
            int x = me.getX();
            int y = me.getY();

            Point p = draw.canvas2view(x/10, y/10);

            status.set(p.x, p.y);
            }
         });

      draw.addMouseListener(new MouseAdapter() {
         @Override
         public void mouseClicked(MouseEvent me) {
            int x = me.getX();
            int y = me.getY();

            Point p = draw.canvas2view(x/10, y/10);
            JOptionPane.showMessageDialog(draw, p.x + ", " + p.y);
            }
         });

      return draw;
      }


   public static void main(String... args) {

      JFrame frame = new JFrame("DigitalMat");

      JMenuBar menu_bar = createMenuBar(frame);
      frame.setJMenuBar(menu_bar);

      frame.setLayout(new BorderLayout());

      JComponent tools = createTools();
      frame.add(BorderLayout.LINE_START, tools);

      StatusBar status = createStatus();
      frame.add(BorderLayout.PAGE_END, status);

      JComponent draw = createDraw(status);
      frame.add(BorderLayout.CENTER, draw);


      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      }

   }

